package com.ungs.form;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.ungs.entities.Proceso;

public class SeleccionarOrdenProcesoForm extends JDialog {
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JComboBox jcbOrden;
	private List<Integer> ordenList;
	private Proceso procesoIni;
	private Proceso procesoReturn;

	public SeleccionarOrdenProcesoForm(JDialog parent, List<Integer> ordenList,
			Proceso proceso) {
		super(parent, true);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.ordenList = ordenList;
		this.procesoIni = proceso;
		initForm();
		cargarComboBox();
	}

	private void initForm() {
		setTitle("Seleccionar Orden");
		setBounds(100, 100, 215, 154);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblNewLabel = new JLabel("Orden");
			lblNewLabel.setBounds(10, 11, 46, 14);
			contentPanel.add(lblNewLabel);
		}
		{
			jcbOrden = new JComboBox();
			jcbOrden.setBounds(10, 33, 66, 22);
			contentPanel.add(jcbOrden);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("Aceptar");
				okButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						eventoBotonAceptar();
					}
				});
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancelar");
				cancelButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						eventoBotonCancelar();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	private void cargarComboBox() {
		for (Integer orden : ordenList) {
			jcbOrden.addItem(orden);
		}
	}

	private void eventoBotonAceptar() {
		int ordenSeleccionado = (Integer) jcbOrden.getSelectedItem();
		procesoIni.setOrden(ordenSeleccionado);
		procesoReturn = procesoIni;
		this.setVisible(false);
	}

	private void eventoBotonCancelar() {
		this.setVisible(false);
	}

	public Proceso getProceso() {
		return procesoReturn;
	}

}
